package com.jfinal.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.jfinal.demo.common.CommonPage;
import com.jfinal.demo.common.CommonResult;
import com.jfinal.demo.model.ShoolInfo;
import com.jfinal.demo.model.ShoolInfoDao;
import com.jfinal.demo.service.ShoolInfoService;
import com.jfinal.plugin.activerecord.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 学校信息控制器
 * @author tanghailan
 * @date 2020-12-17 15:12
 */
@RestController
@Slf4j
@CrossOrigin
public class ShoolInfoController {
    @Resource
    private ShoolInfoService shoolInfoService;

    @GetMapping("/getList")
    public CommonResult getList(@RequestParam(defaultValue = "1") int pageNumber,
                                @RequestParam(defaultValue = "7") int pageSize){
        Page<ShoolInfo> shoolInfoList = shoolInfoService.getShoolInfoList(pageNumber, pageSize);
        return CommonResult.success(CommonPage.restPage(shoolInfoList));
    }

    @PostMapping("/add")
    public CommonResult add(@RequestParam(value = "sh_city") String shCity,
                            @RequestParam(value = "sh_shool") String shShool){
        //参数校验
        if (StrUtil.isEmpty(shCity) || StrUtil.isEmpty(shShool)){
            return CommonResult.validateFailed();
        }
        return CommonResult.success(shoolInfoService.addShoolInfo(shCity,shShool));
    }

    @DeleteMapping("/delete/{id}")
    public CommonResult delete(@PathVariable Long id){
        log.info("id为:"+id);
        //参数校验
        if (id <=0){
            return CommonResult.validateFailed();
        }
        return CommonResult.success(shoolInfoService.deleteShoolInfo(id));
    }

    @GetMapping("/getById/{id}")
    public CommonResult getById(@PathVariable Long id){
        log.info("id为:"+id);
        //参数校验
        if (id <=0){
            return CommonResult.validateFailed();
        }
        ShoolInfo shoolInfo = shoolInfoService.getById(id);
        return CommonResult.success(shoolInfo);
    }

    @PutMapping("/update")
    public CommonResult update(@RequestParam(value = "sh_id") String shId,
                               @RequestParam(value = "sh_city") String shCity,
                               @RequestParam(value = "sh_shool") String shShool){
        //参数校验
        if (StrUtil.isEmpty(shCity) || StrUtil.isEmpty(shShool) || StrUtil.isEmpty(shId)){
            return CommonResult.validateFailed();
        }
        int  iShId = Integer.parseInt(shId);
        return CommonResult.success(shoolInfoService.updateShoolInfo(iShId,shCity,shShool));
    }
}
